/*** 
 * @Author: Leung
 * @Date: 2025-08-07 22:00:44
 * @LastEditTime: 2025-08-07 23:14:43
 * @FilePath: \Projecte:\STM32F103ZET6\STM32_Project_Peripheral_modules\EEPROM_AT24C02\I2C_Hardware\User\i2c\bsp_i2c_ee.h
 * @Description: 
 * @
 * @Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
 */
#ifndef __BSP_I2C_EE_H
#define __BSP_I2C_EE_H

#include "stm32f10x.h"

/*  初始化I2C相关的GPIO
* EEPROM对应SCL引脚为PB6，SDA引脚为PB7，而PB6、PB7引脚对应的复用功能分别是I2C1_SCL(7)、I2C1_SDA(7)
* I2C外设有两个分别I2C1、I2C2，且都在APB1总线下，时钟频率为36MHz
* 开启I2C1外设时钟并使能，开启GPIOB外设时钟并使能，输出模式设置为开漏输出，GPIOB外设在APB2总线下
*/

/**************************I2C参数定义，I2C1或I2C2********************************/
#define EEPROM_I2Cx                         I2C1

#define EEPROM_I2C_CLK                      RCC_APB1Periph_I2C1
#define EEPROM_I2C_APBxClock_FUN            RCC_APB1PeriphClockCmd

#define EEPROM_I2C_GPIO_CLK                 RCC_APB2Periph_GPIOB
#define EEPROM_I2C_GPIO_APBxClock_FUN       RCC_APB2PeriphClockCmd

#define EEPROM_I2C_GPIO_SCL_PORT            GPIOB
#define EEPROM_I2C_GPIO_SCL_PIN             GPIO_Pin_6
#define EEPROM_I2C_GPIO_SDA_PORT            GPIOB
#define EEPROM_I2C_GPIO_SDA_PIN             GPIO_Pin_7


/* STM32 I2C 快速模式 */
#define EEPROM_I2C_CLK_SPEED                400000

// I2C外设地址，有两个I2C_OwnAddress1（7位或10位）、I2C_OwnAddress2（只能配置7位）
/* 这个地址只要与STM32外挂的I2C器件地址不一样即可 */
#define STM32_I2C_OWN_ADDR                  0x5F

/* AT24C01/02每页有8个字节 */
#define I2C_PageSize                        8
/* AT24C04/08A/16A每页有16个字节 */
//#define I2C_PageSize                      16	


/* 等待超时事件 */
#define I2CT_FLAG_TIMEOUT                   ((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT                   ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))


/*信息输出，调试宏*/
#define EEPROM_DEBUG_ON                     1
#define EEPROM_INFO(fmt,arg...)           printf("\r\n<<-EEPROM-INFO->> "fmt"\n",##arg)
#define EEPROM_ERROR(fmt,arg...)          printf("\r\n<<-EEPROM-ERROR->> "fmt"\n",##arg)
#define EEPROM_DEBUG(fmt,arg...)          do{\
                                          if(EEPROM_DEBUG_ON)\
                                          printf("\r\n<<-EEPROM-DEBUG->> [%d]"fmt"\n",__FUNC__,__FILE__,__LINE__, ##arg);\
                                          }while(0)


/* 
 * AT24C02 2kb = 2048bit = 2048/8 B = 256 B
 * 32 pages of 8 bytes each
 *
 * Device Address
 * 1 0 1 0 A2 A1 A0 R/W
 * 1 0 1 0 0  0  0  0 = 0XA0
 * 1 0 1 0 0  0  0  1 = 0XA1 
 */

/* EEPROM Addresses defines */
// EEPROM 从设备地址（8位带上读写位0写1读，0xA0为写，0xA1为写）
#define EEPROM_Block0_ADDRESS 0xA0   /* E2 = 0 */
//#define EEPROM_Block1_ADDRESS 0xA2 /* E2 = 0 */
//#define EEPROM_Block2_ADDRESS 0xA4 /* E2 = 0 */
//#define EEPROM_Block3_ADDRESS 0xA6 /* E2 = 0 */


void I2C_EE_Init(void);
uint32_t EEPROM_Byte_Write(uint8_t *pBuffer, uint8_t WriteAddr);
uint32_t EEPROM_Page_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint8_t NumByteToWrite);
void EEPROM_Buffer_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
uint32_t EEPROM_Buffer_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t numByteToRead);
void EEPROM_WaitForWriteEnd(void);

#endif /* __BSP_I2C_EE_H */
